// SubsqtDiagPage.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "SubsqtDiagPage.h"
#include "../Common/CommonFun.h"
#include "../Common/GYDataSet.h"
#include "PatMan.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSubsqtDiagPage dialog


CSubsqtDiagPage::CSubsqtDiagPage(CWnd* pParent /*=NULL*/)
	: CPageDialog(CSubsqtDiagPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSubsqtDiagPage)
	//}}AFX_DATA_INIT
	m_bDraftSave		= TRUE;
	m_nDiagSN			= -1;

	m_pGaitDraw		= NULL;
	memset(&m_BasicPara, 0, sizeof(m_BasicPara));

	ZERO_ARRAY(m_adwGaitSnap);
	m_nStartPos		= 0;
	m_dwStiOpen		= 0;
	m_dwStiClose	= 0;

	m_bTextChangeFromGait = FALSE;
}

CSubsqtDiagPage::~CSubsqtDiagPage()
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->StopDraw();
		SAFE_DELETE(m_pGaitDraw);
	}

	GetApp()->SetGaitWindow(NULL);
}

void CSubsqtDiagPage::DoDataExchange(CDataExchange* pDX)
{
	CPageDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubsqtDiagPage)
	DDX_Control(pDX, IDC_SPIN_SDIAG_OPEN, m_spinSdiagOpen);
	DDX_Control(pDX, IDC_SPIN_SDIAG_CLOSE, m_spinSdiagClose);
	DDX_Control(pDX, IDC_TAB_BC_SDIAG, m_tabSdiagBC);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSubsqtDiagPage, CPageDialog)
	//{{AFX_MSG_MAP(CSubsqtDiagPage)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_BC_SDIAG, OnSelchangeTabBcSdiag)
	ON_BN_CLICKED(IDB_BUTTON_SDIAG_DRAFTSAVE, OnButtonSdiagDraftsave)
	ON_BN_CLICKED(IDB_BUTTON_SDIAG_SAVE, OnButtonSdiagSave)
	ON_BN_CLICKED(IDC_BUTTON_SNAP, OnButtonSnap)
	ON_BN_CLICKED(IDC_BUTTON_SDIAG_CONTINUE, OnButtonSdiagContinue)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SDIAG_CLOSE, OnDeltaposSpinSdiagClose)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SDIAG_OPEN, OnDeltaposSpinSdiagOpen)
	ON_EN_CHANGE(IDC_EDIT_SDIAG_STICLOSE, OnChangeEditSdiagSticlose)
	ON_EN_CHANGE(IDC_EDIT_SDIAG_STIOPEN, OnChangeEditSdiagStiopen)
	//}}AFX_MSG_MAP
	ON_MESSAGE(GYWA_MSG_REALTIME_OPEN_CLOSE_VALUE, OnRefreshOpenClose)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubsqtDiagPage message handlers

BOOL CSubsqtDiagPage::OnInitDialog() 
{
	CPageDialog::OnInitDialog();

	m_spinSdiagOpen.SetRange(MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);
	m_spinSdiagOpen.SetBuddy(GetDlgItem(IDC_EDIT_SDIAG_STIOPEN));	
	
	m_spinSdiagClose.SetRange(MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);
	m_spinSdiagClose.SetBuddy(GetDlgItem(IDC_EDIT_SDIAG_STICLOSE));	
	
	CButton* pRadio = (CButton*)GetDlgItem(IDC_RADIO_SDIAG_MAN);
	pRadio->SetCheck(TRUE);

	m_pageMiniMental.Create(	CBCMiniMentalPage::SD_IDD,			this,	0);
	m_pageTinettiBal.Create(	CBCTinettiBalPage::SD_IDD,			this,	1);
	m_pageBergScore.Create(		CBCBergScorePage::SD_IDD,			this,	2);
	m_pageFuglMeyerUp.Create(	CBCFuglMeyerUpPage::SD_IDD,			this,	3);
	m_pageFuglMeyerLow.Create(	CBCFuglMeyerLowPage::SD_IDD,		this,	4);
	m_pageHoldenWalk.Create(	CBCHoldenWalkPage::SD_IDD,			this,	5);
	m_pageFuglMeyerBal.Create(	CBCFuglMeyerBalPage::SD_IDD,		this,	6);
	m_pageReviseAshworth.Create(CBCReviseAshworthPage::SD_IDD,		this,	7);
	m_pageBrunnstrom.Create(	CBCBrunnstromPage::SD_IDD,			this,	8);
	m_pageDoctorWrite.Create(	CBCDoctorWritePage::SD_IDD,			this,	9);
	
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 0, GetResString(IDS_BCTAB_MINI_MENTAL),				0, (LPARAM)&m_pageMiniMental);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 1, GetResString(IDS_BCTAB_TINETTI_BAL),				0, (LPARAM)&m_pageTinettiBal);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 2, GetResString(IDS_BCTAB_BERG_SCORE),				0, (LPARAM)&m_pageBergScore);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 3, GetResString(IDS_BCTAB_FUGL_MEYER_UPPER),			0, (LPARAM)&m_pageFuglMeyerUp);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 4, GetResString(IDS_BCTAB_FUGL_MEYER_LOWER),			0, (LPARAM)&m_pageFuglMeyerLow);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 5, GetResString(IDS_BCTAB_HOLDEN_WALK),				0, (LPARAM)&m_pageHoldenWalk);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 6, GetResString(IDS_BCTAB_FUGL_MEYER_LOWER_BAL),		0, (LPARAM)&m_pageFuglMeyerBal);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 7, GetResString(IDS_BCTAB_REVISE_ASHWORTH),			0, (LPARAM)&m_pageReviseAshworth);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 8, GetResString(IDS_BCTAB_BRUNNSTROM),				0, (LPARAM)&m_pageBrunnstrom);
	m_tabSdiagBC.InsertItem(TCIF_TEXT|TCIF_PARAM, 9, GetResString(IDS_BCTAB_DOCTOR_WRITE),				0, (LPARAM)&m_pageDoctorWrite);
	m_tabSdiagBC.SetItemSize(250);
	
	CRect rectTab;
	m_tabSdiagBC.GetWindowRect(&rectTab);
	CRect rectItem;
	m_tabSdiagBC.GetItemRect(0, &rectItem);
	
	int nPageLeft = 17;
	int nPageTop = 20 + rectItem.Height();
	int nPageWidth = rectTab.Width() - 10;
	int nPageHeight = rectTab.Height() - rectItem.Height() - 10;
	CRect rectBCPage(nPageLeft, nPageTop, nPageLeft + nPageWidth, nPageTop + nPageHeight);
	
	VERIFY(m_pageMiniMental.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_SHOWWINDOW));
	VERIFY(m_pageTinettiBal.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageBergScore.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerUp.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerLow.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageHoldenWalk.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerBal.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageReviseAshworth.SetWindowPos(	&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageBrunnstrom.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageDoctorWrite.SetWindowPos(		&m_tabSdiagBC, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	
	m_tabSdiagBC.SetActiveTabColor(ACTIVE_TAB_COLOR2);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSubsqtDiagPage::OnSelchangeTabBcSdiag(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int nCurSel = m_tabSdiagBC.GetCurSel();
	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			pPage->ShowWindow(i == nCurSel ? SW_SHOW : SW_HIDE);
		}
	}
	
	*pResult = 0;
}

void CSubsqtDiagPage::OnButtonSdiagDraftsave() 
{
	BOOL bIsPaused = FALSE;
	if(m_pGaitDraw)
		bIsPaused = m_pGaitDraw->IsDrawPaused();

	StopRealTimeGait();

	m_bDraftSave = TRUE;

	if(SaveData(GetApp()->GetCurMRN()))
	{
		GYMsgBox(IDS_PAT_DRAFT_SAVE_OK, MB_ICONINFORMATION);
	}

	ShowRealTimeGait();

	if(m_pGaitDraw)
		m_pGaitDraw->PauseDraw(bIsPaused);
}

void CSubsqtDiagPage::OnButtonSdiagSave() 
{
	DWORD dwMRNCode = GetApp()->GetCurSN();
	if(dwMRNCode == 0)
		return;

	CString strStiOpen;
	GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->GetWindowText(strStiOpen);
	int nStiOpen = _ttoi(strStiOpen);
	
	CString strStiClose;
	GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->GetWindowText(strStiClose);
	int nStiClose = _ttoi(strStiClose);
	
	if(!IsNumInRange(nStiOpen, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE) || !IsNumInRange(nStiClose, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		CString strFormat = GetResString(IDS_PAT_OPEN_CLOSE_RANGE);
		CString strInfo;
		strInfo.Format(strFormat, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);
		
		GYMsgBox(strInfo, MB_OK|MB_ICONEXCLAMATION);
		return;
	}


#ifdef THIGH_WA_MODE
	if(_ttoi(strStiOpen) >= _ttoi(strStiClose))
	{
		GYMsgBox(IDS_PAT_OPEN_LESS_CLOSE, MB_OK|MB_ICONEXCLAMATION);
		return;
	}
#else
	if(_ttoi(strStiClose) >= _ttoi(strStiOpen))
	{
		GYMsgBox(IDS_PAT_CLOSE_LESS_OPEN, MB_OK|MB_ICONEXCLAMATION);
		return;
	}
#endif

	if(!CGaitAnaly::HasSdiagSnapGait(dwMRNCode, m_nDiagSN))
	{
		if(GYMsgBox(IDS_PAT_NO_GAIT, MB_YESNO|MB_ICONQUESTION) != IDYES)
			return;
	}

	if(IsAllBodyCheckBlank())
	{
		if(GYMsgBox(IDS_PAT_ALLBLANK_CONTINUE, MB_YESNO|MB_ICONQUESTION) != IDYES)
			return;
	}

	if(GYMsgBox(IDS_PAT_CONFIRM_SAVE, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;

	StopRealTimeGait();

	m_bDraftSave = FALSE;

	if(SaveData(GetApp()->GetCurMRN()))
	{
		GYMsgBox(IDS_GBL_SAVE_SUCCESS, MB_ICONINFORMATION);
		EnableInputControls(FALSE);
	}
}

BOOL CSubsqtDiagPage::SaveData(CString strMRN)
{
	BOOL bContinue = TRUE;
	if(bContinue)
		bContinue = SaveMedInfo(strMRN, m_bDraftSave);
	
	if(bContinue)
		bContinue = SaveBodyCheck(strMRN, m_bDraftSave);

	if(bContinue && !m_bDraftSave)
		bContinue = WriteWAParams(strMRN);

	CPatMan* pParent = (CPatMan*)GetParent();
	if(bContinue && pParent)
	{
		CPageDialog* pSDiagQuery = pParent->GetTabPage(TAB_INDEX_SDQUERY);
		if(pSDiagQuery)
			pSDiagQuery->ShowTableData(GetApp()->GetCurMRN());
	}
	
	return bContinue;
}

BOOL CSubsqtDiagPage::WriteWAParams(CString strMRN)
{
	if(!ReadWABasicParam())
		return FALSE;

	CString strUpTime;
	CWnd* pUpTime = GetDlgItem(IDC_EDIT_SDIAG_UPTIME);
	pUpTime->GetWindowText(strUpTime);
	
	CString strDownTime;
	CWnd* pDownTime = GetDlgItem(IDC_EDIT_SDIAG_DOWNTIME);
	pDownTime->GetWindowText(strDownTime);
	
	CString strStiOpen;
	CWnd* pStiOpen = GetDlgItem(IDC_EDIT_SDIAG_STIOPEN);
	pStiOpen->GetWindowText(strStiOpen);
	
	CString strStiClose;
	CWnd* pStiClose = GetDlgItem(IDC_EDIT_SDIAG_STICLOSE);
	pStiClose->GetWindowText(strStiClose);
	
	double dUpTime		= _tcstod(strUpTime, NULL);
	double dDownTime	= _tcstod(strDownTime, NULL);
	DWORD dwStiClose	= _ttoi(strStiClose);
	DWORD dwStiOpen		= _ttoi(strStiOpen);
	
	WA_BASIC_PARA wbp = {0};
	wbp.dwSize			= sizeof(WA_BASIC_PARA);
	wbp.dwPulseFrq		= m_BasicPara.dwPulseFrq;
	wbp.dwPulseWidth	= m_BasicPara.dwPulseWidth;
	wbp.dwPulseCount	= m_BasicPara.dwPulseCount;
	wbp.dwStiUpTime		= (dUpTime		* 100);
	wbp.dwStiDownTime	= (dDownTime	* 100);
	wbp.dwOpenThr		= (dwStiOpen	& 0xff);
	wbp.dwCloseThr		= (dwStiClose	& 0xff);
	
	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nIndex = GetApp()->GetCurrentWAIndex();
	
	BOOL bWBP = FALSE;
	WRITE_WA_MORE_TIMES(bWBP, pBTMan->cmdWriteParamOCD(nIndex, wbp));

	if(!bWBP)
	{
		GYMsgBox(IDS_SYS_WRITE_PARAM_ERROR, MB_ICONSTOP);
		return FALSE;
	}
	
	if(pBTMan->GetWACommVersion(nIndex) >= 0x12)
	{
		DWORD dwMRNCode = GetApp()->GetCurSN();

		BOOL bMRN = FALSE;
		WRITE_WA_MORE_TIMES(bMRN, pBTMan->cmdWriteMRNCode(nIndex, dwMRNCode));

		if(!bMRN)
		{
			GYMsgBox(IDS_PAT_WRITE_MRNCODE_ERROR, MB_ICONSTOP);
		}
	}
	
	m_BasicPara = wbp;	// update the basic parameters
	return TRUE;
}

BOOL CSubsqtDiagPage::ShowTableData(CString strMRN)
{
	//  get the last diagnose SN
	CString strSQL;
	strSQL.Format(_T("SELECT DiagSN FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN > 0 ORDER BY DiagSN DESC LIMIT 1"), strMRN);

	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		TCHAR szTabTitle[MAX_PATH] = {0};
		_tcscpy(szTabTitle, GetResString(IDS_TABTITLE_SUBSEDIAG));

		CPatMan* pParent = (CPatMan*)GetParent();
		pParent->SetTabTitle(TAB_INDEX_SUBSDIAG, szTabTitle);

		for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
		{
			TCITEM tcItem = {0};
			tcItem.mask = TCIF_PARAM;
			m_tabSdiagBC.GetItem(i, &tcItem);
			
			CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
			if(pPage)
				pPage->ClearData();
		}

		return FALSE;
	}

	CString strDiagSN;
	setDiagRec.GetFieldValue(_T("DiagSN"), strDiagSN);

	m_nDiagSN = _ttoi(strDiagSN);

	BOOL bIsDraft = FALSE;
	BOOL bRet = ShowTableDataByDiagSN(strMRN, m_nDiagSN, &bIsDraft);

	EnableInputControls(bIsDraft);

	return bRet;
}

BOOL CSubsqtDiagPage::ShowTableDataByDiagSN(CString strMRN, int nDiagSN, BOOL* pbIsDraft)
{
	// show medical info
	BOOL bIsDraft = FALSE;
	if(!ShowMedInfoByDiagSN(strMRN, nDiagSN, &bIsDraft))
		return FALSE;

	CString strCheckDate;
	CString strSQL;
	strSQL.Format(_T("SELECT DiagDate FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN=%d"), strMRN, nDiagSN);
	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		CTime tCur = CTime::GetCurrentTime();
		strCheckDate = tCur.Format(_T("%Y-%m-%d"));
	}
	else
	{
		setDiagRec.GetFieldValue(_T("DiagDate"), strCheckDate);
		strCheckDate = strCheckDate.Left(10);
	}
	setDiagRec.Close();

	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			pPage->ClearData();
			pPage->ShowTableData(strMRN, nDiagSN);
			pPage->SetLastColTitile(strCheckDate, nDiagSN);
		}
	}

	TCHAR szTabTitle[MAX_PATH] = {0};
	_stprintf(szTabTitle, _T("%s %d"), GetResString(IDS_TABTITLE_SUBSEDIAG), nDiagSN);

	CPatMan* pParent = (CPatMan*)GetParent();
	pParent->SetTabTitle(TAB_INDEX_SUBSDIAG, szTabTitle);

	if(bIsDraft)
	{
		ShowRealTimeGait();
	}
	else
	{
		StopRealTimeGait();
		SetTimer(GYWA_MSG_SDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
	}

	*pbIsDraft = bIsDraft;

	return TRUE;
}

BOOL CSubsqtDiagPage::Prepare()
{
	GetApp()->SetGaitWindow(GetSafeHwnd());
	
	if(!IsControlsEnable())
	{
		SetTimer(GYWA_MSG_SDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
	}
	else
	{
		if(m_pGaitDraw && m_pGaitDraw->IsDrawPaused())
			SetTimer(GYWA_MSG_1STDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
	}

	return TRUE;
}

BOOL CSubsqtDiagPage::ClearData()
{
	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
			pPage->ClearData();
	}

	GetDlgItem(IDC_EDIT_SUMMARY)->SetWindowText(_T(""));

	m_nDiagSN = -1;
	
	return TRUE;
}

BOOL CSubsqtDiagPage::EnableInputControls(BOOL bEnable)
{
	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
			pPage->EnableInputControls(bEnable);
	}
	
	GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_SDIAG_CONTINUE)->EnableWindow(FALSE);

	GetDlgItem(IDC_RADIO_SDIAG_AUTO)->EnableWindow(bEnable);

	GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->EnableWindow(bEnable);
	GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->EnableWindow(bEnable);

	GetDlgItem(IDC_SPIN_SDIAG_OPEN)->EnableWindow(bEnable);
	GetDlgItem(IDC_SPIN_SDIAG_CLOSE)->EnableWindow(bEnable);

	GetDlgItem(IDC_EDIT_SUMMARY)->EnableWindow(bEnable);
	
	GetDlgItem(IDB_BUTTON_SDIAG_DRAFTSAVE)->EnableWindow(bEnable);
	GetDlgItem(IDB_BUTTON_SDIAG_SAVE)->EnableWindow(bEnable);

	CPageDialog::EnableInputControls(bEnable);
	return TRUE;
}


BOOL CSubsqtDiagPage::NewSdiag(int nNewDiagSN)
{
	if(nNewDiagSN < 1)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	CPatMan* pParent = (CPatMan*)GetParent();
	NULL_RETURN_FALSE(pParent);

	pParent->EnableTab(TAB_INDEX_SUBSDIAG, TRUE);
	pParent->SelectTab(TAB_INDEX_SUBSDIAG);

	TCHAR szTabTitle[MAX_PATH] = {0};
	_stprintf(szTabTitle, _T("%s %d"), GetResString(IDS_TABTITLE_SUBSEDIAG), nNewDiagSN);
	pParent->SetTabTitle(TAB_INDEX_SUBSDIAG, szTabTitle);

	// Clear data before data!
	ClearData();

	CTime tCur = CTime::GetCurrentTime();
	CString strToday = tCur.Format(_T("%Y-%m-%d"));

	//  show 2 previous body check result
	CString strMRN = GetApp()->GetCurMRN();
	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			if(nNewDiagSN > 1)
				pPage->ShowTableDataWithStartCol(strMRN, nNewDiagSN - 1, 1);

			pPage->SetLastColTitile(strToday, nNewDiagSN);
		}
	}

	EnableInputControls(TRUE);
	m_nDiagSN = nNewDiagSN;

	CWnd* pDoctorName = GetDlgItem(IDC_EDIT_DOCTOR);
	pDoctorName->SetWindowText(GetApp()->GetUserRealName());

	if(ShowParamsFromWA())
	{
		ShowRealTimeGait();
		return TRUE;
	}

	return FALSE;
}

BOOL CSubsqtDiagPage::ShowParamsFromWA()
{
	if(!ReadWABasicParam())
		return FALSE;
	
	CString strFreq;
	strFreq.Format(_T("%.1f"), 100.0/m_BasicPara.dwPulseFrq);
	
	CEdit* pStiRate = (CEdit*)GetDlgItem(IDC_EDIT_SDIAG_FREQ);
	pStiRate->SetWindowText(strFreq);
	
	CString strWidth;
	strWidth.Format(_T("%d"), m_BasicPara.dwPulseWidth * 50);
	
	CEdit* pBanDWidth = (CEdit*)GetDlgItem(IDC_EDIT_SDIAG_WIDTH);
	pBanDWidth->SetWindowText(strWidth);
	
	CString strCount;
	strCount.Format(_T("%d"), m_BasicPara.dwPulseCount);
	CEdit* pPulesNum = (CEdit*)GetDlgItem(IDC_EDIT_SDIAG_COUNT);
	pPulesNum->SetWindowText(strCount);

	CString strUpTime;
	strUpTime.Format(_T("%.1f"), m_BasicPara.dwStiUpTime / 100.0);
	GetDlgItem(IDC_EDIT_SDIAG_UPTIME)->SetWindowText(strUpTime);
	
	CString strDownTime;
	strDownTime.Format(_T("%.1f"), m_BasicPara.dwStiDownTime / 100.0);
	GetDlgItem(IDC_EDIT_SDIAG_DOWNTIME)->SetWindowText(strDownTime);
	
	CString strTriggerOpen;
	strTriggerOpen.Format(_T("%d"), m_BasicPara.dwOpenThr);
	GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->SetWindowText(strTriggerOpen);
	
	CString strTriggerClose;
	strTriggerClose.Format(_T("%d"), m_BasicPara.dwCloseThr);
	GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->SetWindowText(strTriggerClose);

	return TRUE;
}


BOOL CSubsqtDiagPage::SaveMedInfo(CString strMRN, BOOL bIsDraft)
{
	//  Also save BlueTooth address
	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nIndex = GetApp()->GetCurrentWAIndex();
	
	CString strWADevID = pBTMan->GetWADevID(nIndex);
	CString strBTAddress = pBTMan->GetWAAddress(nIndex);

	CString strPulseFreq;
	CWnd* pPulseFreq = GetDlgItem(IDC_EDIT_SDIAG_FREQ);
	pPulseFreq->GetWindowText(strPulseFreq);
	
	CString strPulseWidth;
	CWnd* pPulseWidth = GetDlgItem(IDC_EDIT_SDIAG_WIDTH);
	pPulseWidth->GetWindowText(strPulseWidth);
	
	CString strPulseCount;
	CWnd* pPulseCount = GetDlgItem(IDC_EDIT_SDIAG_COUNT);
	pPulseCount->GetWindowText(strPulseCount);
	
	CString strUpTime;
	CWnd* pUpTime = GetDlgItem(IDC_EDIT_SDIAG_UPTIME);
	pUpTime->GetWindowText(strUpTime);
	
	CString strDownTime;
	CWnd* pDownTime = GetDlgItem(IDC_EDIT_SDIAG_DOWNTIME);
	pDownTime->GetWindowText(strDownTime);
	
	CString strStiOpen;
	CWnd* pStiOpen = GetDlgItem(IDC_EDIT_SDIAG_STIOPEN);
	pStiOpen->GetWindowText(strStiOpen);
	
	CString strStiClose;
	CWnd* pStiClose = GetDlgItem(IDC_EDIT_SDIAG_STICLOSE);
	pStiClose->GetWindowText(strStiClose);
	
	CString strSummary;
	CWnd* pSummary = GetDlgItem(IDC_EDIT_SUMMARY);
	pSummary->GetWindowText(strSummary);

	//  replace ' to ''
	strSummary.Replace(_T("'"), _T("''"));

	double dUpTime = _tcstod(strUpTime, NULL);
	double dDownTime = _tcstod(strDownTime, NULL);
	DWORD dwStiClose = _ttoi(strStiClose);
	DWORD dwStiOpen = _ttoi(strStiOpen);
	
	CTime curTime = CTime::GetCurrentTime();
	CString strCurTime;
	strCurTime.Format(_T("%4d-%02d-%02d %02d:%02d:%02d"),	curTime.GetYear(),	curTime.GetMonth(),		curTime.GetDay(),
															curTime.GetHour(),	curTime.GetMinute(),	curTime.GetSecond());
	
	CString strExist;
	strExist.Format(_T("SELECT COUNT(*) AS RecCnt FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN=%d"), strMRN, m_nDiagSN);
	CGYDataSet setDiagRec(strExist);

	BOOL bExist = FALSE;
	if(!setDiagRec.IsEOF())
	{
		CString strRecCnt;
		setDiagRec.GetFieldValue(_T("RecCnt"), strRecCnt);
		if(strRecCnt != _T("0"))
			bExist = TRUE;
	}

	CString strRealName = GetApp()->GetUserName();

	CString strSQL;
	if(bExist)
	{
		strSQL.Format(_T("UPDATE DiagRecord SET DiagDate='%s', UserName='%s', IsDraft=%d, UpTime=%.1f, DownTime=%.1f, StiOpen=%d, StiClose=%d, PulseFreq=%s, PulseWidth=%s, PulseCount=%s, WADevID='%s', WAAddress='%s', DiagSummary='%s' WHERE MedicalRecordNo='%s' AND DiagSN=%d"),
												strCurTime,		strRealName,	bIsDraft,	dUpTime,	dDownTime,		dwStiOpen, dwStiClose,	strPulseFreq, strPulseWidth, strPulseCount, strWADevID,	 strBTAddress,		strSummary,						strMRN,		m_nDiagSN);
	}
	else
	{
		strSQL.Format(_T("INSERT INTO DiagRecord (MedicalRecordNo, DiagSN, DiagDate, UserName, IsDraft, UpTime, DownTime, StiOpen, StiClose, PulseFreq, PulseWidth, PulseCount, WADevID, WAAddress, DiagSummary) VALUES ('%s', %d, '%s', '%s', %d, %.1f, %.1f, %d, %d, %s, %s, %s, '%s', '%s', '%s')"),
						strMRN, m_nDiagSN, strCurTime, strRealName, bIsDraft, dUpTime, dDownTime, dwStiOpen, dwStiClose, strPulseFreq, strPulseWidth, strPulseCount, strWADevID, strBTAddress, strSummary);
	}
	
	CString strSQL2;
	strSQL2.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_SUBSDIAG, GetApp()->GetCurMRN());
	
	if(!setDiagRec.ExecuteSQL(strSQL) || !setDiagRec.ExecuteSQL(strSQL2))
	{
		DataErrorMsgBox(__FILE__, __LINE__, strSQL + _T("\r\n\r\n") + strSQL2, setDiagRec.GetLastErrorString());
		return FALSE;
	}

	return TRUE;
}

BOOL CSubsqtDiagPage::SaveBodyCheck(CString strMRN, BOOL bIsDraft)
{
	BOOL bContinue = TRUE;
	for(int i = 0; bContinue && i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			ASSERT(m_nDiagSN > 0);
			
			int nRet = pPage->SaveData(strMRN, m_nDiagSN, bIsDraft);
			if(nRet == SaveBCE_Succ)
			{
				bContinue = TRUE;
			}
			else if(nRet == SaveBCE_PageBlank)
			{
				bContinue = TRUE;
			}
			else
			{
				bContinue = FALSE;
			}
		}
	}

	return bContinue;
}

BOOL CSubsqtDiagPage::ShowMedInfoByDiagSN(CString strMRN, int nDiagSN, BOOL* pbIsDraft)
{
	if(nDiagSN < 1 || pbIsDraft == NULL)
		return FALSE;

	CString strSQL;
	strSQL.Format(_T("SELECT DiagDate, RealName, UpTime, IsDraft, DownTime, StiOpen, StiClose, PulseFreq, PulseWidth, PulseCount, WADevID, DiagSummary FROM DiagRecord a, UserInfo b WHERE a.MedicalRecordNo='%s' AND a.DiagSN=%d AND a.UserName=b.UserName ORDER BY DiagSN"), strMRN, nDiagSN);
	
	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		GYMsgBox(IDS_GBL_DATA_ERROR, MB_ICONSTOP);
		return FALSE;
	}

	CString strValue;

	setDiagRec.GetFieldValue(_T("RealName"), strValue);
	GetDlgItem(IDC_EDIT_DOCTOR)->SetWindowText(strValue);

	setDiagRec.GetFieldValue(_T("UpTime"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_UPTIME)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("DownTime"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_DOWNTIME)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("StiOpen"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("StiClose"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("PulseFreq"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_FREQ)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("PulseWidth"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_WIDTH)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("PulseCount"), strValue);
	GetDlgItem(IDC_EDIT_SDIAG_COUNT)->SetWindowText(strValue);
	
	setDiagRec.GetFieldValue(_T("DiagSummary"), strValue);
	GetDlgItem(IDC_EDIT_SUMMARY)->SetWindowText(strValue);

	setDiagRec.GetFieldValue(_T("IsDraft"), strValue);
	*pbIsDraft = (strValue != _T("0"));
	
	return TRUE;
}


void CSubsqtDiagPage::OnButtonSnap() 
{
	DWORD dwMRNCode = GetApp()->GetCurSN();
	
	if(m_pGaitDraw)
	{
		m_pGaitDraw->GetGaitSnap(m_adwGaitSnap, &m_nStartPos, &m_dwStiOpen, &m_dwStiClose);
		m_pGaitDraw->SnapGait(dwMRNCode, m_nDiagSN);
		m_pGaitDraw->PauseDraw(TRUE);
		
		GetDlgItem(IDC_BUTTON_SDIAG_CONTINUE)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(FALSE);
	}
}

BOOL CSubsqtDiagPage::ReadWABasicParam()
{
	if(m_BasicPara.dwSize != sizeof(WA_BASIC_PARA))
	{
		CWAManager* pBTMan = GetApp()->GetWAMan();
		int nIndex = GetApp()->GetCurrentWAIndex();
		
		for(int i = 0; i < 3 && m_BasicPara.dwSize != sizeof(WA_BASIC_PARA); i++)
		{
			m_BasicPara = pBTMan->cmdGetBasicPara(nIndex);
			
			if(i < 2)
				Sleep(500);
		}
		
		if(	m_BasicPara.dwSize != sizeof(WA_BASIC_PARA) ||
			m_BasicPara.dwPulseFrq == 0					||
			m_BasicPara.dwPulseWidth == 0				||
			m_BasicPara.dwPulseCount == 0				)
		{
			pBTMan->RemoveDeviceFromList(nIndex);

			GYMsgBox(IDS_PAT_READ_PARAM_FAIL, MB_OK|MB_ICONSTOP);

			CPatMan* pParent = (CPatMan*)GetParent();
			if(pParent)
			{
				pParent->QuitPatMan();
				pBTMan->SearchWAImmediately();
			}

			return FALSE;
		}

		if(m_BasicPara.dwPulseFrq < 3 || m_BasicPara.dwPulseFrq > 6)
		{
			if(m_BasicPara.dwPulseFrq == 0)
				m_BasicPara.dwPulseFrq = 1;
			
			CString strFormat = GetResString(IDS_SYS_PULSE_FREQ_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, 100.0/m_BasicPara.dwPulseFrq);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}
		
		if(m_BasicPara.dwPulseWidth < 1 || m_BasicPara.dwPulseWidth > 5)
		{
			CString strFormat = GetResString(IDS_SYS_PULSE_WIDTH_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, m_BasicPara.dwPulseWidth * 50);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}
	}

	return (m_BasicPara.dwSize == sizeof(WA_BASIC_PARA));
}

BOOL CSubsqtDiagPage::ShowStaticGaitSnap()
{
	//  Show Static Gait Snap
	int nStartPos = 0;
	int nTriggerOpen = 0;
	int nTriggerClose = 0;
	
	DWORD dwGaitPoints[GAIT_POINT_ARRAY_SIZE] = {0};
	CString strDevID;
	CString strDevAddress;
	BOOL bRet = CGaitAnaly::ReadGaitFromTable(GetApp()->GetCurSN(), m_nDiagSN, _T(""), dwGaitPoints, &nStartPos, &nTriggerOpen, &nTriggerClose, &strDevID, &strDevAddress);
	if(!bRet)
		return FALSE;
	
	WA_BASIC_PARA wbp = {0};
	wbp.dwSize = sizeof(WA_BASIC_PARA);
	wbp.dwOpenThr = nTriggerOpen;
	wbp.dwCloseThr = nTriggerClose;
	
	CRect rectCtrl;
	GetDlgItem(IDC_STATIC_SQTDIAG_GAIT)->GetClientRect(&rectCtrl);
	
	CDC* pDC = GetDlgItem(IDC_STATIC_SQTDIAG_GAIT)->GetDC();
	
	CGaitAnaly ga;
	ga.InitGait(pDC, rectCtrl, dwGaitPoints, nStartPos);
	ga.SetGaitPara(&wbp);
	ga.SetDevID(strDevID);
	ga.BeginDrawPrintGaitGraph(0, 0);
	ga.EndDrawPrintGaitGraph();
	
	pDC->DeleteDC();	

	return TRUE;
}

BOOL CSubsqtDiagPage::ShowRealTimeGait()
{
	//  if first diag is working, stop the gait first
	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent)
	{
		CFirstDiagPage* pFirstDiag = (CFirstDiagPage*)pParent->GetTabPage(TAB_INDEX_1STDIAG);
		if(pFirstDiag)
			pFirstDiag->ClearUp();
	}
	
	if(!ReadWABasicParam())
		return FALSE;

	if(m_pGaitDraw == NULL)
	{
		m_pGaitDraw = new CGaitAnaly((CStatic*)GetDlgItem(IDC_STATIC_SQTDIAG_GAIT), GetApp()->GetCurrentWAIndex());
		m_pGaitDraw->SetGaitPara(&m_BasicPara);
		m_pGaitDraw->StartDraw();
	}
	
	return TRUE;
}

BOOL CSubsqtDiagPage::StopRealTimeGait()
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->StopDraw();
		SAFE_DELETE(m_pGaitDraw);
	}
	
	return TRUE;
}

void CSubsqtDiagPage::ShowGaitTimerFunc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
	CSubsqtDiagPage* pThis = (CSubsqtDiagPage*)CSubsqtDiagPage::FromHandle(hWnd);
	if(pThis)
	{
		pThis->KillTimer(nIDEvent);

		if(pThis->m_pGaitDraw == NULL)
			CGaitAnaly::ShowStaticGaitSnap(pThis->GetDlgItem(IDC_STATIC_SQTDIAG_GAIT), pThis->m_nDiagSN);
		else
			pThis->DrawPausedStaticGait();
	}
}

BOOL CSubsqtDiagPage::ClearUp()
{
	return StopRealTimeGait();
}

void CSubsqtDiagPage::OnRefreshOpenClose(WPARAM wParam, LPARAM lParam)
{
	CButton* pParamAuto = (CButton*)GetDlgItem(IDC_RADIO_SDIAG_AUTO);
	ASSERT(pParamAuto);
	
	if(pParamAuto->GetCheck())
	{
		m_bTextChangeFromGait = TRUE;

		CString strTriggerOpen;
		strTriggerOpen.Format(_T("%d"), wParam);
		GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->SetWindowText(strTriggerOpen);
		
		CString strTriggerClose;
		strTriggerClose.Format(_T("%d"), lParam);
		GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->SetWindowText(strTriggerClose);

		if(m_pGaitDraw)
		{
			WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
			wbp.dwOpenThr = wParam;
			wbp.dwCloseThr= lParam;
			
			m_pGaitDraw->SetGaitPara(&wbp);
			
			DrawPausedStaticGait();
		}
	}
}

void CSubsqtDiagPage::OnButtonSdiagContinue() 
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->PauseDraw(FALSE);
	}
	
	GetDlgItem(IDC_BUTTON_SDIAG_CONTINUE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(TRUE);
}

void CSubsqtDiagPage::OnDeltaposSpinSdiagClose(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	if(pNMUpDown)
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_SDIAG_STIOPEN);
		ASSERT(pOpenEdit);

		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_SDIAG_STICLOSE);
		ASSERT(pCloseEdit);
		
		pOpenEdit->GetWindowText(strOpenValue);
		pCloseEdit->GetWindowText(strCloseValue);
		
		int nOpenValue = _ttoi(strOpenValue);
		int nCloseValue = _ttoi(strCloseValue);
		
		if(pNMUpDown->iDelta > 0)
		{
			nCloseValue++;
			if(nCloseValue > MAX_OPEN_CLOSE_VALUE)
				nCloseValue = MAX_OPEN_CLOSE_VALUE;
			
#ifdef THIGH_WA_MODE
#else
			if(nCloseValue >= (nOpenValue - 1))
				nCloseValue = (nOpenValue - 1);
#endif
		}
		else if(pNMUpDown->iDelta < 0)
		{
			nCloseValue--;
			if(nCloseValue < MIN_OPEN_CLOSE_VALUE)
				nCloseValue = MIN_OPEN_CLOSE_VALUE;

#ifdef THIGH_WA_MODE
			if(nCloseValue <= (nOpenValue + 1))
				nCloseValue = (nOpenValue + 1);
#endif
		}
		
		strCloseValue.Format(_T("%d"), nCloseValue);
		pCloseEdit->SetWindowText(strCloseValue);
		
		OnChangeEditSdiagSticlose();
	}

	*pResult = 0;
}

void CSubsqtDiagPage::OnDeltaposSpinSdiagOpen(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	if(pNMUpDown)
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_SDIAG_STIOPEN);
		ASSERT(pOpenEdit);
		
		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_SDIAG_STICLOSE);
		ASSERT(pCloseEdit);
		
		pOpenEdit->GetWindowText(strOpenValue);
		pCloseEdit->GetWindowText(strCloseValue);
		
		int nOpenValue = _ttoi(strOpenValue);
		int nCloseValue = _ttoi(strCloseValue);
		
		if(pNMUpDown->iDelta > 0)
		{
			nOpenValue++;
			if(nOpenValue > MAX_OPEN_CLOSE_VALUE)
				nOpenValue = MAX_OPEN_CLOSE_VALUE;
			
#ifdef THIGH_WA_MODE
			if(nOpenValue >= (nCloseValue - 1))
				nOpenValue = (nCloseValue - 1);
#endif
		}
		else if(pNMUpDown->iDelta < 0)
		{
			nOpenValue--;
			if(nOpenValue < MIN_OPEN_CLOSE_VALUE)
				nOpenValue = MIN_OPEN_CLOSE_VALUE;
			
#ifdef THIGH_WA_MODE
#else
			if(nOpenValue <= (nCloseValue + 1))
				nOpenValue = (nCloseValue + 1);
#endif
		}
		
		strOpenValue.Format(_T("%d"), nOpenValue);
		pOpenEdit->SetWindowText(strOpenValue);
		
		OnChangeEditSdiagStiopen();
	}
	
	*pResult = 0;
}

void CSubsqtDiagPage::OnChangeEditSdiagSticlose() 
{
	if(m_bTextChangeFromGait)
	{
		m_bTextChangeFromGait = FALSE;
		return;
	}
	
	CString strCloseValue;
	GetDlgItem(IDC_EDIT_SDIAG_STICLOSE)->GetWindowText(strCloseValue);
	
	int nCloseValue = _ttoi(strCloseValue);
	if(m_pGaitDraw && IsNumInRange(nCloseValue, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
		
		wbp.dwCloseThr = nCloseValue;
		m_pGaitDraw->SetGaitPara(&wbp);

		DrawPausedStaticGait();
	}
}

void CSubsqtDiagPage::OnChangeEditSdiagStiopen() 
{
	if(m_bTextChangeFromGait)
	{
		m_bTextChangeFromGait = FALSE;
		return;
	}
	
	CString strOpenValue;
	GetDlgItem(IDC_EDIT_SDIAG_STIOPEN)->GetWindowText(strOpenValue);
	
	int nOpenValue = _ttoi(strOpenValue);
	if(m_pGaitDraw && IsNumInRange(nOpenValue, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
		
		wbp.dwOpenThr = nOpenValue;
		m_pGaitDraw->SetGaitPara(&wbp);

		DrawPausedStaticGait();
	}
}

BOOL CSubsqtDiagPage::DrawPausedStaticGait()
{
	if(m_pGaitDraw && m_pGaitDraw->IsDrawPaused())
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_SDIAG_STIOPEN);
		ASSERT(pOpenEdit);
		
		pOpenEdit->GetWindowText(strOpenValue);
		m_dwStiOpen = _ttoi(strOpenValue);
		
		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_SDIAG_STICLOSE);
		ASSERT(pCloseEdit);
		
		pCloseEdit->GetWindowText(strCloseValue);
		m_dwStiClose = _ttoi(strCloseValue);
		
		CWAManager* pBTMan = GetApp()->GetWAMan();
		int nIndex = GetApp()->GetCurrentWAIndex();
		CString strWAID = pBTMan->GetWADevID(nIndex);
		CString strDevAddress = pBTMan->GetWAAddress(nIndex);

		CWnd* pGaitLabel = GetDlgItem(IDC_STATIC_SQTDIAG_GAIT);
		CGaitAnaly::ShowGaitInCtrl(pGaitLabel, m_adwGaitSnap, m_nStartPos, m_dwStiOpen, m_dwStiClose, strWAID, strDevAddress);
		
		return TRUE;
	}
	
	return FALSE;
}

void CSubsqtDiagPage::SetRealtimeState()
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->PauseDraw(FALSE);

		GetDlgItem(IDC_BUTTON_SDIAG_CONTINUE)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(TRUE);
	}
}

BOOL CSubsqtDiagPage::IsAllBodyCheckBlank()
{
	BOOL bAllPageBlank = TRUE;
	for(int i = 0; i < m_tabSdiagBC.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabSdiagBC.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage && !pPage->IsPageCurDayBlank())
		{
			bAllPageBlank = FALSE;
			break;
		}
	}

	return bAllPageBlank;
}